package com.qk.controller;

import com.qk.anno.Log;
import com.qk.common.PageResult;
import com.qk.common.Result;
import com.qk.entity.Dept;
import com.qk.service.DeptService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Slf4j
@RestController
public class DeptController {
    @Autowired
    private DeptService deptService;
@Log
    @PostMapping("/depts")
    public Result addDept(@RequestBody Dept dept) {
        log.info("添加部门：{}", dept);
        deptService.add(dept);
        return Result.success();
    }
    //两边查询
    @GetMapping("/depts")
    public Result list(String name, Integer status,
                      @RequestParam(defaultValue = "1") Integer page,
                       @RequestParam(defaultValue = "10") Integer pageSize) {
        log.info("查询部门，参数：name={},status={},page={},pageSize={}", name, status, page, pageSize);
        //调用service ，分页查询
        PageResult<Dept> pageResult = deptService.list(name, status, page, pageSize);
        return Result.success(pageResult);
    }
    @GetMapping("/depts/{id}")
    public Result detail(@PathVariable Integer id) {
        log.info("查询部门详情，参数：id={}", id);
        Dept dept = deptService.getById(id);
        return Result.success(dept);
    }
    @Log
    @PutMapping("/depts")
    public Result update(@RequestBody Dept dept) {
        log.info("修改部门，参数：{}", dept);

        deptService.update(dept);
        return Result.success();
    }
    @Log
    @DeleteMapping("/depts/{id}")
    public Result delete(@PathVariable Integer id) {
        log.info("删除部门，参数：{}", id);

        deptService.deleteById(id);
        return Result.success();
    }

    //查询所有的部门
    /**
     * 查询所有正常状态的部门（不分页）
     * GET /depts/list
     */
    @GetMapping("/depts/list")
    public Result listAllNormal() {
        log.info("查询所有正常状态的部门");
        List<Dept> list = deptService.listAllNormal();
        return Result.success(list);
    }




}
